package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.Part;

import java.util.ArrayList;
import java.util.Locale;

/**
 * Created by Administrator on 2015/6/10.
 */
public class PartListAdapter extends BaseAdapter {

    private ArrayList<Part> mParts;
    private ArrayList<Part> mPartsTemp = new ArrayList<>();
    public PartListAdapter(ArrayList<Part> parts){
        this.mParts = parts;
    }
    public void setmBrandsTemps(ArrayList<Part> parts){
        if (parts != null)
        {
            mPartsTemp.clear();
            mPartsTemp.addAll(parts);
        }
    }

    public void addBrand(Part part){
        if (part != null)
        {
            mPartsTemp.add(part);
        }
    }

    @Override
    public int getCount() {
        return mParts == null? 0:mParts.size();
    }

    @Override
    public Part getItem(int position) {
        return mParts == null? null:mParts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.part_list_item, parent, false);
            holder.mSelectView = (ImageView)convertView.findViewById(R.id.iv_select);
            holder.mTitle = (TextView)convertView.findViewById(R.id.part_title);
            holder.mPrice = (TextView)convertView.findViewById(R.id.part_price);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }


        Part part = getItem(position);

        if (part != null)
        {

            if (part.bSelect)
                holder.mSelectView.setVisibility(View.VISIBLE);
            else
                holder.mSelectView.setVisibility(View.INVISIBLE);

            holder.mTitle.setText(part.title);

            holder.mPrice.setText("￥"+part.suggestPrice);
        }



        return convertView;
    }

    public void filter(String charText) {
        if (charText != null)
        {
            charText = charText.toLowerCase(Locale.getDefault());
            mParts.clear();
            if (charText.length() == 0) {
                mParts.addAll(mPartsTemp);
            } else {
                for (Part part : mPartsTemp) {
                    if (part.title.toLowerCase(Locale.getDefault()).contains(charText)) {
                        mParts.add(part);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }


    public static class ViewHolder{
        public ImageView mSelectView;
        public TextView mTitle;
        public TextView mPrice;
    }
}
